/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title 阻塞队列测试（读取本地文件）
 * @Description 键入 本地文件夹目录+ 关键字 -> 找到文件夹下 所有名字匹配关键的文件名称并输出
 * @Package concurrent.blockingQueue
 * @date 2021/3/19 4:58 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package concurrent.blockingQueue.filesearch;

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class FileSearchTest {

    public static final String EMPTY = "";

    public static void main(String[] args) throws InterruptedException {
        System.out.println("[file_search]  请输入你要搜索的文件夹绝对路径");
        Scanner in = new Scanner(System.in);
        String directory = in.nextLine();

        if (directory.equals(EMPTY)) {
            System.out.println("[file_search] 文件夹路径为空，程序退出");
            return;
        }
        System.out.print("进入指定目录目录：" + directory);
        System.out.print("键入关键字：");
        String keyword = in.nextLine();
        if (keyword.equals(EMPTY)) {
            System.out.println("[file_search] 关键字为空，程序退出");
            return;
        }

        final int FILE_QUEUE_SIZE = 10000;
        final int SEARCH_THREADS = 10;

        BlockingQueue<File> queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE);

        FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory));
        Thread enumeratorTask = new Thread(enumerator);
        enumeratorTask.start();

        //等待文件搜索5秒
        TimeUnit.SECONDS.sleep(5);

        //多线程搜索 十个线程
        for (int i = 0; i < SEARCH_THREADS; i++) {
            //起一个新线程搜索
            new Thread(new SearchTask(queue, keyword)).start();
        }


    }

//    FileInputStream
//    BufferedInputStream bis = new BufferedInputStream();
//    BufferedOutputStream bos = new BufferedOutputStream();
//    BufferedReader bufferedReader = new BufferedReader();
//    BufferedWriter bufferedWriter = new BufferedWriter();
}
